package com.mouliu.base.concurrent17;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author mouliu
 * @create 2018-04-15-下午8:57
 */
public class UseThreadPoolExecutor2 implements Runnable {
    private static AtomicInteger count = new AtomicInteger();

    @Override
    public void run() {
        try {
            int temp = count.incrementAndGet();
            System.out.println("任务"+temp);
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) throws InterruptedException {
        BlockingQueue<Runnable>queue
                = new ArrayBlockingQueue<>(10);

        ExecutorService executor =
                new ThreadPoolExecutor(5,10,120L,TimeUnit.SECONDS,queue);

        for (int i=0;i<20;i++){
            executor.execute(new UseThreadPoolExecutor2());
        }
        Thread.sleep(1000);
        System.out.println("queue size:"+queue.size());//10
        Thread.sleep(2000);
    }
}
